package com.itheima.controller.user;

import com.itheima.dto.OrdersPageQueryDTO;
import com.itheima.dto.OrdersPaymentDTO;
import com.itheima.dto.OrdersSubmitDTO;
import com.itheima.service.OrderService;
import com.itheima.vo.OrderSubmitVO;
import com.itheima.vo.OrderVO;
import com.itheima.result.PageBean;
import com.itheima.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

@RestController("userOrderController")
@RequestMapping("/user/order")
@Slf4j
@Api(tags = "C端-订单接口")
public class OrderController {


    @Autowired
    private OrderService orderService;

    /**
     * 提交订单
     * @param ordersSubmitDTO
     * @return
     */
    @PostMapping("/submit")
    public Result showOrder(@RequestBody OrdersSubmitDTO ordersSubmitDTO){

       OrderSubmitVO orderSubmitVO= orderService.submit(ordersSubmitDTO);
       return Result.success(orderSubmitVO);
    }

    /**
     * 订单支付
     * @param ordersPaymentDTO
     * @return
     */

    @PutMapping("/payment")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO){
       LocalDateTime estimatedDeliveryTime= orderService.getByNumber(ordersPaymentDTO);
        return Result.success(estimatedDeliveryTime);
    }

    /**
     * 历史订单查询
     * @param ordersPageQueryDTO
     * @return
     */
    @GetMapping("/historyOrders")
    public Result page(OrdersPageQueryDTO ordersPageQueryDTO){
       PageBean pageBean= orderService.pageHistoryOrders(ordersPageQueryDTO);
       return Result.success(pageBean);
    }

    /**
     * 查询订单详情
     * @param id
     * @return
     */
    @GetMapping("/orderDetail/{id}")
    public Result<OrderVO> findById(@PathVariable Long id){
        log.info("id为:{}",id);
        OrderVO order= orderService.findById(id);
        return Result.success(order);
    }

    /**
     * 取消订单
     * @param id
     * @return
     */
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable Long id){
        log.info("id为：{}==============",id);
        orderService.cancelOrder(id);
        return Result.success();
    }

    /**
     * 再来一单
     * @param id
     * @return
     */
    @PostMapping("/repetition/{id}")
    public Result addAgainOrder(@PathVariable Long id){
        log.info("id为：{}",id);
         orderService.addAgain(id);
         return Result.success();

    }

    @GetMapping("/reminder/{id}")
    @ApiOperation("用户催单")
    public Result reminder(@PathVariable("id") Long id) {
        orderService.reminder(id);
        return Result.success();
    }


}